package com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.view.textview.EllipsezeTextView;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/6/14.
 */

public class VideoDiscoverHolder extends BaseDiscoverHolder {

    private ImageView content;
    private TextView pageNum;
    private EllipsezeTextView intro;
    private TextView title;

    public VideoDiscoverHolder(View itemView) {
        super(itemView);
        content = (ImageView) itemView.findViewById(R.id.ground_content_img);
        pageNum = (TextView) itemView.findViewById(R.id.ground_pagenum_tv);
        intro = (EllipsezeTextView) itemView.findViewById(R.id.ground_intro_tv);
        title = (TextView) itemView.findViewById(R.id.daily_teamname_tv);
    }

    @Override
    public void renderData(final Timeline mItem, final Context mContext) {
        super.renderData(mItem, mContext);
        ImageLoader.getInstance().displayImage(mItem.getContentAddress(), content, ImageOptions.optionsTimeline, null, null, false);
        if (!TextUtils.isEmpty(mItem.getTitle())) {
            title.setVisibility(View.VISIBLE);
            title.setText(Html.fromHtml(mItem.getTitle()));
        } else {
            title.setVisibility(View.GONE);
        }
        try {
            String time = StringUtil.getTimeDuration((int) Double.parseDouble(mItem.getVideo_info().getDuration()));
            pageNum.setText(time);
            pageNum.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            pageNum.setVisibility(View.GONE);
        }
        setRecommendIntro(mContext, intro, mItem, StringUtil.htmlDecoder(mItem.getSummary()), mContext.getString(R.string.post_video));
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoDailyDetail(mContext, mItem.getUd_id(), false);
            }
        });
        intro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailDataHelper.gotoDailyDetail(mContext, mItem.getUd_id(), false);
            }
        });
    }

    private void setRecommendIntro(Context mContext, EllipsezeTextView tvIntro, Timeline mItem, String intro, String defaultIntro) {
        String finalIntro = TextUtils.isEmpty(intro) ? defaultIntro : intro;
        finalIntro = String.format(mContext.getResources().getString(R.string.card_recommended_user_format), mItem.getOUName(), finalIntro);
        tvIntro.setText(finalIntro, mItem.getOUName(), mItem.getOuid(), true);
        tvIntro.setVisibility(View.VISIBLE);
    }
}
